<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GroupController
 *
 * @author Huỳnh
 */

class Admin_User_GroupController extends My_Controller_Action {
	public function init(){
		parent::init();
		
		$auth = Zend_Auth::getInstance();
        
        if(!$auth->hasIdentity())
        { 
            $this->_redirect("user/account/login");
        }
        
        if ($this->isAccess($auth->getIdentity()->group_id, 8))
        {
			//to do
		} else {
            die("Access denied");
        }
	}
    
    public function indexAction()
    {
    	$this->view->rightAdminModules('right');
    	
    	$this->view->by = $this->_request->getParam("by", "group_name");
        $this->view->order = $this->_request->getParam("order", "asc");
        	
        Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
    	$model = $this->loadModel('admin_user', 'Group');
		$this->view->paginator = $model->getAllGroup($this->_getParam('page', 1), $this->view->by.' '.$this->view->order);
    }
    
    public function editAction()
    {
    	$this->view->rightAdminModules('right');

		$this->view->errors = $this->_errorMessenger->getMessages();
		$this->_errorMessenger->clearMessages();
		
		// get group id
    	$group_id = $this->_request->getParam("id");
    	if($group_id == null)
    	{
    		$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "group", "action" => "index"));
			$this->_redirect($redirect_url);
    	}
    	
    	// get group
    	$model = $this->loadModel("admin_user", "Group");
    	$this->view->group = $model->getGroupById($group_id);
    	$this->view->group->role = explode(";", $this->view->group->role);
    	
    	// get list control
		$model = $this->loadModel("admin_user", "Control");
		$this->view->controls = $model->getAllControl();
    }
    
    public function editpostAction()
    {
    	// get params
    	$group_name = $this->_request->getParam("group_name");
    	$group_id = $this->_request->getParam("group_id");
    	$controls = $this->_request->getParam("control");
    	
    	// name empty
    	if($group_name == "")
    	{
    		$this->_errorMessenger->addMessage("Tên không đúng");
    		$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "group", "action" => "edit"));
    		$redirect_url .= "?id=" .  $group_id;
			$this->_redirect($redirect_url);
    	}
    	
    	// update
    	$model = $this->loadModel("admin_user", "Group");
    	$group = $model->getGroupById($group_id);
    	$group->group_name = $group_name;
    	$group->role = join(";", $controls);
    	$group->save();
    	
    	$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "group", "action" => "index"));
    	$this->_redirect($redirect_url);
    }
    
    public function addAction()
    {
    	$this->view->rightAdminModules('right');

		$this->view->errors = $this->_errorMessenger->getMessages();
		$this->_errorMessenger->clearMessages();
		
		// get list control
		$model = $this->loadModel("admin_user", "Control");
		$this->view->controls = $model->getAllControl();
    }
    
    public function addpostAction()
    {
    	// get params
    	$group_name = $this->_request->getParam("group_name");
    	$controls = $this->_request->getParam("control");
    	
    	// name empty
    	if($group_name == "")
    	{
    		$this->_errorMessenger->addMessage("Tên không đúng");
    		$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "group", "action" => "add"));
			$this->_redirect($redirect_url);
    	}
    	
    	// add group
    	$model = $this->loadModel("admin_user", "Group");
    	$group = $model->createRow();
    	$group->group_name = $group_name;
    	$group->role = join(";", $controls);
    	$group->save();
    	
    	$redirect_url = $this->getUrl(array("module" => "admin_user", "controller" => "group", "action" => "index"));
    	$this->_redirect($redirect_url);
    }
    
    public function deleteAction()
    {
    	// get param
    	$group_id = $this->_request->getParam("id");
    	if($group_id == null)
    	{
    		$this->_redirect("admin_user/group");
    	}
    	
    	// delete group
    	$model = $this->loadModel("admin_user", "Group");
    	$group = $model->getGroupById($group_id);
    	$group->delete();
    	
    	$this->_redirect("admin_user/group");
    }
}

?>
